<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2019/7/15
 * Time: 8:54
 */

namespace app\admin\controller;

use think\Db;

class Sensor extends Common
{
    /**
     * @return array|mixed
     * 传感器类型列表
     */
    public function SensorTypeList()
    {
        if (request()->isPost()) {
            $key = input('post.key') ? input('key') : '';
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $list = Db('nyd_sensor_type')
                ->where('is_del', 1)
                ->where('name', 'like', "%" . $key . "%")
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }

        return $this->fetch('sensor_type_list');
    }


    /**
     * 添加
     */
    public function AddSensorType()
    {

        if (request()->isPost()) {
            $data = input('post.');

            $data['range'] = $data['range_min'] . "~" . $data['range_max'];

            $info['range'] = $data['range'];
            $info['name'] = $data['name'];
            $info['img'] = $data['img'];

            $res = Db::name('nyd_sensor_type')
                ->where('name', $info['name'])
                ->where('is_del', 1)
                ->find();
            if ($res) {
                return $this->error("传感器类型已存在");
            } //插入
            else {
                $re = Db::name('nyd_sensor_type')->insert($info);
                if ($re) {
                    $this->success('添加成功');
                } else {
                }
                $this->error('添加失败');
            }
        }
        return $this->fetch('sensor_type_add');
    }

    /**
     * chuanganqibianji ---wangpengyang
     * 2019-7-15
     */
    public function EditSensorType()
    {

        if (request()->isPost()) {
            $data = input('post.');

            $data['range'] = $data['range_min'] . "~" . $data['range_max'];

            $sensor['range'] = $data['range'];
            $sensor['name'] = $data['name'];
            $sensor['img'] = $data['img'];

            $re = Db::name('nyd_sensor_type')->where('id', $data['id'])->update($sensor);

            if ($re) {
                $this->success('修改成功');
            } else {
                $this->error('修改失败');
            }

        }

        $id = input('get.id');
        if (empty($id)) {
            return $this->error('传感器类型不存在!');
        }

        $result = Db::name('nyd_sensor_type')
            ->where('is_del', 1)
            ->where('id', $id)
            ->find();
        $range_all = explode('~', $result['range']);
        $result['range_min'] = $range_all[0];
        $result['range_max'] = $range_all[1];

        $this->assign('info', $result);
        $this->assign('id', $id);
        return $this->fetch('sensor/sensor_type_edit');
    }

    /**
     * shanchu
     */
    public function SensorTypeDel()
    {
        $id = input('post.id');
        if (empty($id)) {
            return $this->error('传感器类型不存在!');
        }
        $is_del = 2;
        db('nyd_sensor_type')->where('id', $id)->update(['is_del' => $is_del]);
        return $this->success('传感器类型删除成功');
    }

    /**
     * zhuangtaigaibianwangpengyang
     */
    public function SensorTypeState()
    {
        $id = input('post.id');
        $is_open = input('post.is_open');
        if (empty($id)) {
            return $this->error('传感器类型不存在!');
        }
        db('nyd_sensor_type')->where('id', $id)->update(['is_open' => $is_open]);
        return $this->success('修改成功');
    }





    /********************************传感器管理*******************************/
    /**
     * chuanganqiliebiaoguangli---wangpengyang2019-7-15 11:31
     */
    public function SensorList()
    {
        if (request()->isPost()) {
            $key = input('post.key') ? input('key') : '';
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $list = Db('nyd_sensor')
                ->alias('a')
                ->join('nyd_sensor_type b', 'a.type_id = b.id')
                ->join('nyd_gateway g', 'g.id = a.gateway_id')
                ->join('nyd_house h', 'h.id = g.house_id')
                ->field('a.id,a.sensor_mac,a.sensor_name,a.is_bad,a.is_open,b.name,b.img,b.range,
                h.house_name,g.gateway_name,g.gateway_id')
                ->where('a.is_del', 1)
                ->where('a.sensor_name|a.sensor_mac', 'like', "%" . $key . "%")
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            foreach ($list['data'] as $key => $value) {
                $list['data'][$key]['range_all'] = explode('~', $list['data'][$key]['range']);
                $list['data'][$key]['range_min'] = $list['data'][$key]['range_all'][0];
                $list['data'][$key]['range_max'] = $list['data'][$key]['range_all'][1];
                if ($list['data'][$key]['is_bad'] == 1) {
                    $list['data'][$key]['is_bad'] = '正常';
                } else if ($list['data'][$key]['is_bad'] == 2) {
                    $list['data'][$key]['is_bad'] = '故障';
                }
            }
// echo '<pre>';
//             print_r($list['data']);
//             die;
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return $this->fetch('sensorList');

    }

    /**
     * @return mixed
     * tianjiachuanganqi
     */
    public function SensorAdd()
    {
        if (request()->isAjax()) {
            if (empty(input('post.type_id'))) {
                $house_id = input('post.') ? input('post.') : '';

                //网关
                $gateway = Db::name('nyd_gateway')->where('house_id', $house_id['house_id'])->select();
                foreach ($gateway as $key => $value) {
                    $gateway[$key]['gateway'] = $gateway[$key]['gateway_id'] . '--' . $gateway[$key]['gateway_name'];
                }
                return $gateway;
            } else {
                if (request()->isPost()) {
                    $data = input('post.');

                    $res = Db::name('nyd_sensor')->where('is_del', 1)->where('sensor_mac', $data['sensor_mac'])->find();
                    if ($res) {
                        $this->error('该传感器编号已存在！');
                    }
                    // $type_id = $data['type_id'];
                    // $senser = Db::name('nyd_sensor_type')->field('range')->where('id',$type_id)->find();
                    // $range_all = explode('-', $senser['range']);
                    // $result['range_min'] = $range_all[0];
                    // $result['range_max'] = $range_all[1];
                    $re = Db::name('nyd_sensor')->insert($data);
                    if ($re) {
                        $this->success('添加成功');
                    } else {
                    }
                    $this->error('添加失败');
                }
            }

        }

        $house = Db::name('nyd_house')->select();
        $this->assign('house', $house);

        $result = Db::name('nyd_sensor_type')->where('is_del', 1)->select();
        $this->assign('res', $result);
        return $this->fetch('sensorAdd');
    }


    /**
     * bianji --wangpengyang--2019-7-15  22:40
     */
    public function SensorEdit()
    {
        if (request()->isAjax()) {
            if (empty(input('post.type_id'))) {
                $house_id = input('post.') ? input('post.') : '';

                //网关
                $gateway = Db::name('nyd_gateway')->where('house_id', $house_id['house_id'])->select();
                foreach ($gateway as $key => $value) {
                    $gateway[$key]['gateway'] = $gateway[$key]['gateway_id'] . '--' . $gateway[$key]['gateway_name'];
                }
                return $gateway;
            } else {
                if (request()->isPost()) {
                    $data = input('post.');
                    // echo '<pre>';
                    // print_r($data);
                    // die;
                    $re = Db::name('nyd_sensor')->where('id', $data['id'])->update($data);
                    if ($re) {
                        $this->success('修改成功');
                    } else {
                        $this->error('修改失败');
                    }

                }
            }

        }

        $id = input('get.id');
        if (empty($id)) {
            return $this->error('传感器类型不存在!');
        }

        $house = Db::name('nyd_house')->select();
        $this->assign('house', $house);
        $result = Db::name('nyd_sensor')
            ->alias('a')
            ->join('nyd_sensor_type b', 'a.type_id = b.id')
            ->join('nyd_gateway g', 'g.id = a.gateway_id')
            ->join('nyd_house h', 'h.id = g.house_id')
            ->field('a.id,a.sensor_mac,a.sensor_name,a.is_bad,a.is_open,b.name,b.img,b.range,a.type_id, h.house_name,g.gateway_name,g.gateway_id,g.house_id,a.gateway_id')
            ->where('a.id', $id)
            ->where('a.is_del', 1)
            ->find();

        $this->assign('info', $result);
        $this->assign('id', $id);

        $rest = Db::name('nyd_sensor_type')->where('is_del', 1)->select();

        $this->assign('res', $rest);
        return $this->fetch('sensor/sensorEdit');
    }


    /**
     * zhuangtaigaibianwangpengyang 2019-7-15 13:01
     */
    public function SensorState()
    {
        $id = input('post.id');
        $is_open = input('post.is_open');
        if (empty($id)) {
            return $this->error('传感器不存在!');
        }
        db('nyd_sensor')->where('id', $id)->update(['is_open' => $is_open]);
        return $this->success('修改成功');
    }


    /**
     * shanchu
     */
    public function SensorDel()
    {
        $id = input('post.id');
        if (empty($id)) {
            return $this->error('传感器不存在!');
        }
        $is_del = 2;
        db('nyd_sensor')->where('id', $id)->update(['is_del' => $is_del]);
        return $this->success('传感器删除成功');
    }


}